package com.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.github.pagehelper.PageInfo;
import com.model.Customer;
import com.model.ReponseDto;
import com.service.ICustomerService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value = "/customer")
@Api(tags = "用户管理", value = "用户管理相关api")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;

    @RequestMapping(value = "/list")
    @ApiOperation(value = "用户列表")
    @ApiImplicitParams({ @ApiImplicitParam(name = "uid", value = "UID", required = false),
            @ApiImplicitParam(name = "userName", value = "用户名", required = false),
            @ApiImplicitParam(name = "invitationCode", value = "邀请码", required = false),
            @ApiImplicitParam(name = "pageNo", value = "pageNo", required = false),
            @ApiImplicitParam(name = "pageSize", value = "pageSize", required = false)})
    public ReponseDto<PageInfo> list(Model model, HttpServletRequest request, String uid, String userName, Integer pageSize,
            String invitationCode, String pageNo) {
        return customerService.list(uid, userName, invitationCode, pageNo, pageSize);
    }

    @RequestMapping(value = "/update")
    @ApiOperation(value = "用户编辑")
    @ApiImplicitParams({})
    public ReponseDto<String> update(Model model, HttpServletRequest request, @RequestBody Customer customer,
            @RequestHeader String userId, @RequestHeader String username) {
        return customerService.edit(customer, userId, username);
    }

    @RequestMapping(value = "/flow/list")
    @ApiOperation(value = "流水列表")
    @ApiImplicitParams({ @ApiImplicitParam(name = "userId", value = "userId", required = true),
            @ApiImplicitParam(name = "startDate", value = "开始时间", required = false),
            @ApiImplicitParam(name = "endDate", value = "结束时间", required = false),
            @ApiImplicitParam(name = "thirdOrderNo", value = "订单号", required = false),
            @ApiImplicitParam(name = "pageSize", value = "pageSize", required = false)})
    public ReponseDto<PageInfo> flowlist(Model model, HttpServletRequest request, Integer userId,Integer pageSize,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate, String thirdOrderNo, String pageNo) {
        return customerService.flowlist(userId, startDate, endDate, thirdOrderNo, pageNo,pageSize);
    }

    @RequestMapping(value = "/relation/list")
    @ApiOperation(value = "用户关系列表")
    @ApiImplicitParams({ @ApiImplicitParam(name = "userId", value = "userId", required = false),
            @ApiImplicitParam(name = "uid", value = "uid", required = false),
            @ApiImplicitParam(name = "searchType", value = "1:向上查询,2:向下查询", required = false),
            @ApiImplicitParam(name = "pageNo", value = "pageNo", required = false),
            @ApiImplicitParam(name = "pageSize", value = "pageSize", required = false)})
    public ReponseDto<PageInfo> relationlist(Model model, HttpServletRequest request, Integer userId, String uid,Integer pageSize,
            String searchType, String pageNo) {
        return customerService.relationlist(userId, uid, searchType, pageNo,pageSize);
    }


    @GetMapping(value = "/search")
    @ApiOperation(value = "用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "用户名", required = false)
    })
    public ReponseDto<List<Customer>> search(@RequestParam(value = "name",required = false) String name) {
        return new ReponseDto<>(customerService.search(name));
    }
}
